//
//  ZPTeacherCommentViewController.m
//  ZPIOSEducation
//
//  Created by Encoder on 17/1/12.
//  Copyright © 2017年 mini1. All rights reserved.
//
#import "ZPTeacherCommentViewController.h"
#import "ZPTeacherCmmTableViewCell.h"
#import "QQAutoSizeLabel.h"
#import "IQKeyboardManager.h"

@interface ZPTeacherCommentViewController ()<UITableViewDataSource,UITableViewDelegate>{
    MBProgressHUD *hud;
    NSMutableArray *tableData;
}
@property (weak, nonatomic) IBOutlet UITableView *cmmTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (nonatomic,strong) QQAutoSizeLabel *autoSizeLable;

@end

@implementation ZPTeacherCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"评价";
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    tableData = [NSMutableArray array];
    _cmmTableView.tableFooterView=[UIView new];
    [_cmmTableView registerNib:[UINib nibWithNibName:@"ZPTeacherCmmTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZPTeacherCmmTableViewCell"];
    [self setupTopView];
    [self getEvalDetail];
    [self getEvalList];
}

-(void)getEvalDetail{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] GET:APISearch@"teacher/getEvalDetail.json" parameters:@{@"teacherId":_teacherId} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            NSArray *result = [responseObject objectForKey:@"result"];
            NSMutableArray *dataA= [NSMutableArray array];
            for (NSDictionary *tmp in result) {
                if ([toStr([tmp objectForKey:@"number"]) isEqualToString:@"0"]) {
                    continue;
                }
                NSDictionary *aaa=@{@"searchWords":[NSString stringWithFormat:@"%@ %@",[tmp objectForKey:@"name"],[tmp objectForKey:@"number"]]};
                [dataA addObject:aaa];
            }
            [self setLabelData:dataA];
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}

-(void)getEvalList{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] GET:APISearch@"teacher/getEvalList.json" parameters:@{@"teacherId":_teacherId} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            NSArray *result = [responseObject objectForKey:@"result"];
            [tableData addObjectsFromArray:result];
            [_cmmTableView reloadData];
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setupTopView{
    _autoSizeLable = [[QQAutoSizeLabel alloc]initWithFrame:CGRectMake(0, 15, SCREENWIDTH-6, 100)];
    [_topView addSubview:_autoSizeLable];
}

-(void)setLabelData:(NSArray *)dataArray{
    [_autoSizeLable setSearchHotViewWithArray:dataArray andSampleLabelFont:11 height:20];
    _autoSizeLable.frameheight = [_autoSizeLable containHeight]+30;
    _heightCons.constant = _autoSizeLable.frameheight;
    _autoSizeLable.hotTagClickBlock = ^(NSUInteger currentIndex,UILabel *lab){
    };
    
    _autoSizeLable.hotTagNameBlock=^(NSString *tagName,CusLab *label){
    };
    
    
    [self.topView setNeedsUpdateConstraints];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return tableData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tmpD = [tableData objectAtIndex:indexPath.row];
    NSString *tmp = [tmpD objectForKey:@"remark"];
    float h = ceilf([tmp calculateSize:CGSizeMake(SCREENWIDTH-80, 9999) font:[UIFont systemFontOfSize:11]].height)+59.5;
    return h;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZPTeacherCmmTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPTeacherCmmTableViewCell"];
    NSDictionary *tmp = [tableData objectAtIndex:indexPath.row];
    if ([[tmp objectForKey:@"isAnonymous"] intValue]==1) {
        cell.stuName.text = @"匿名";
[cell.headUrl setImage: [UIImage imageNamed:@"headportrait"]];
    }else{
        cell.stuName.text = [tmp objectForKey:@"stuName"];
[cell.headUrl sd_setImageWithURL:[tmp objectForKey:@"headUrl"] placeholderImage:[UIImage imageNamed:@"headportrait"]];
    }
    
    cell.updateTime.text=[tmp objectForKey:@"updateTime"];
    cell.cmmContent.text = [tmp objectForKey:@"remark"];
    cell.teachingLevelScore = [[tmp objectForKey:@"teachingLevelScore"] intValue];
    return cell;
}

@end
